<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:350px;overflow:auto">
                    <!-- <sc-editor :disabled="mode=='show'" v-model="form.reason_id" placeholder="请输入" :height="350"></sc-editor> -->
                    <el-form-item label="退货原因" prop="reason_id">
                        <el-select v-model="form.reason_id" filterable placeholder="请选择退货原因" clearable multiple style="width:100%">
                            <el-option v-for="item in returnList" :key="item.id" :label="item.title" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="文字描述" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入文字描述" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="质检报告" prop="images">
                        <sc-upload-multiple v-model="form.images" draggable></sc-upload-multiple>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible=false" >取 消</el-button>
			        <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
// import { defineAsyncComponent } from 'vue';
// const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
        // components: {scEditor},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加客户',
					edit: '编辑客户',
					show: '退货'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
					order_no:"",
                    reason_id:[],
                    images:"",
                    remark:"",
				},
				//验证规则
				rules: {},
                returnList:[],
			}
		},
		mounted() {
            this.getReturn()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.order.getOrder.refundReason.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                console.log('data :>> ', data);
                this.form.order_no = data.order_no
                this.form.reason_id = data.returnReason!=null?data.returnReason.reason_id:""
                this.form.images = data.returnReason!=null?data.returnReason.images:""
                this.form.remark = data.returnReason!=null?data.returnReason.remark:""
			},
            // 获取退货原因
            async getReturn(){
                var res = await this.$API.all.returnReason.get();
                this.returnList = res.data;
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
